package com.dyp.mybatisdemo.service;


import com.dyp.mybatisdemo.model.po.Person;
import com.dyp.mybatisdemo.repository.PersonMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class PersonService {
    private Logger logger = LoggerFactory.getLogger(PersonService.class);

    @Autowired
    PersonMapper personMapper;

    @Transactional
    public Person getPersonByFirstCache(String id){
        logger.debug(personMapper.selectByPrimaryKeyWithoutSecondCache(id).toString());
        logger.debug(personMapper.selectByPrimaryKeyWithoutSecondCache(id).toString());
        return personMapper.selectByPrimaryKeyWithoutSecondCache(id);
    }

    //没有事务的时候一级缓存是不生效的，也间接证明了如果多次调用，只要是跨事务了一级缓存就失效了
    public Person getPersonWithoutCache(String id){
        logger.debug(personMapper.selectByPrimaryKeyWithoutSecondCache(id).toString());
        logger.debug(personMapper.selectByPrimaryKeyWithoutSecondCache(id).toString());
        return personMapper.selectByPrimaryKeyWithoutSecondCache(id);
    }

    //二级缓存优先与一级缓存
    public Person getPersonWithSecondCache(String id){
        logger.debug(personMapper.selectByPrimaryKeyWithSecondCache(id).toString());;
        logger.debug(personMapper.selectByPrimaryKeyWithSecondCache(id).toString());;
        return personMapper.selectByPrimaryKeyWithSecondCache(id);
    }
}
